package com.socialuni.center.web.user.login;

import com.socialuni.center.web.entity.ThirdUserLoginEntity;
import com.socialuni.center.web.factory.CenterMineUserDetailROFactory;
import com.socialuni.sdk.manage.UserFollowDetailManage;
import com.socialuni.center.web.manage.CenterUserDetailManage;
import com.socialuni.sdk.model.DO.user.UserFollowDetailDO;
import com.socialuni.center.web.model.DO.oAuth.ThirdUserTokenDO;
import com.socialuni.center.web.model.DO.user.DevAccountDO;
import com.socialuni.center.web.model.DO.user.UserDetailDO;
import com.socialuni.center.web.model.EO.ThirdUserLoginEO;
import com.socialuni.center.web.model.EO.UserDetailLoginEO;
import com.socialuni.center.web.model.RO.CenterLoginRO;
import com.socialuni.center.web.model.RO.user.CenterMineUserDetailRO;
import com.socialuni.sdk.repository.UserFollowDetailRepository;
import com.socialuni.center.web.repository.user.UserDetailRepository;
import com.socialuni.center.web.utils.DevAccountUtils;
import com.socialuni.sdk.constant.AuthType;
import com.socialuni.sdk.model.DO.user.UserDO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
@Slf4j
public class CenterLoginEntity {
    @Resource
    private ThirdUserLoginEntity thirdUserLoginEntity;
    @Resource
    private UserFollowDetailRepository userFollowDetailRepository;
    @Resource
    private UserDetailRepository userDetailRepository;
    @Resource
    private UserFollowDetailManage userFollowDetailManage;
    @Resource
    private CenterUserDetailManage centerUserDetailManage;

    //创建用户详情信息，并生成token，返回记录
    public CenterLoginRO<CenterMineUserDetailRO> centerLogin(UserDO mineUser) {
        //创建或返回
        UserFollowDetailDO userFollowDetailDO = userFollowDetailRepository.findByUserId(mineUser.getId());
        UserDetailLoginEO userDetailLoginEO;
        //如果为空则用户详情和follow都为空
        if (userFollowDetailDO == null) {
            UserDetailDO userDetailDO = centerUserDetailManage.createUserDetail(mineUser);
            userFollowDetailDO = userFollowDetailManage.createUserDetailFollow(mineUser);
            userDetailLoginEO = new UserDetailLoginEO(userDetailDO, userFollowDetailDO);
        } else {
            UserDetailDO userDetailDO = userDetailRepository.findOneByUserId(mineUser.getId());
            userDetailLoginEO = new UserDetailLoginEO(userDetailDO, userFollowDetailDO);
        }
        CenterMineUserDetailRO userDetailRO = CenterMineUserDetailROFactory.providerLoginGetMineUserDetail(mineUser, userDetailLoginEO);

        //获取开发者对应的账号
        DevAccountDO devAccountDO = DevAccountUtils.getDevAccount();

        ThirdUserLoginEO thirdUserLoginEO = thirdUserLoginEntity.thirdUserLogin(devAccountDO, mineUser, AuthType.user);
        //授权成功
        //token有效期三个月
        //生成userToken
        ThirdUserTokenDO tokenDO = thirdUserLoginEO.getTokenDO();

        //生成返回对象
        CenterLoginRO<CenterMineUserDetailRO> userDetailROUniLoginRO = new CenterLoginRO<>();
        userDetailROUniLoginRO.setTokenCode(tokenDO.getToken());
        userDetailROUniLoginRO.setToken(tokenDO.getToken());
        userDetailROUniLoginRO.setUser(userDetailRO);
        return userDetailROUniLoginRO;
    }

}
